// pages/login/login.js
const app = getApp()
import {
  request
} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 自定义nav
    statusBarHeight1: app.globalData.statusBarHeight + 45,
    showUser: true,
    showPhone: false,
    openId: '',
    accessToken: '',
    userId: '',
    nickName: '',
    headimg: '',
    key: '',
    // 如果是邀请进来的  会有fId
    fId: ''
  },
  // 返回首页
  goIndex() {
    wx.switchTab({
      url: '/pages/tabbars/home/home',
    })
  },
  bindGetUserInfo(e) {
    console.log(e);
    if (e.detail.iv) {
      this.setData({
        showPhone: true,
        showUser: false,
        nickName: e.detail.userInfo.nickName,
        headimg: e.detail.userInfo.avatarUrl
      })
      // ===
      // request({
      //   url: '/zhongyisheji-applet/login/userLogion',
      //   method: 'post',
      //   data: {
      //     openid: this.data.openId,
      //     nickName: this.data.nickName,
      //     headimg: this.data.headimg,
      //     phone: '15518187881'
      //   }
      // }).then(res1 => {
      //   console.log(res1);
      //   if(res1.code == 200) {
      //     wx.showToast({
      //       title: '登录成功',
      //     })
      //     let obj = {
      //       accessToken: res1.data.accessToken,
      //       userId: res1.data.userId
      //     }
      //     wx.setStorage({
      //       data: obj,
      //       key: 'userInfo',
      //     })
      //     // 登陆成功后跳转到首页
      //     wx.switchTab({
      //       url: '/pages/tabbars/home/home',
      //     })
      //   }
      // })
    }

  },
  bindGetPhone(e) {
    console.log(e);
    if (e.detail.iv) {
      request({
        url: '/zhongyisheji-applet/login/getPhone',
        method: 'post',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          session_key: this.data.key
        }
      }).then(res => {
        console.log(res);
        if (res.phoneNumber) {
          request({
            url: '/zhongyisheji-applet/login/userLogion',
            method: 'post',
            data: {
              openid: this.data.openId,
              nickName: this.data.nickName,
              headimg: this.data.headimg,
              phone: res.phoneNumber
            }
          }).then(res1 => {
            console.log(res1);
            if (res1.code == 200) {
              wx.showToast({
                title: '登录成功',
              })
              let obj = {
                accessToken: res1.data.accessToken,
                userId: res1.data.userId
              }
              wx.setStorage({
                data: obj,
                key: 'userInfo',
              })
              if(this.data.fId) {
                request({
                  url: '/zhongyisheji-applet/public/shareInto',
                  method: 'post',
                  data: {
                    superiorId: this.data.fId,
                    openid: this.data.openId,
                    nickName: this.data.nickName,
                    phone: res.phoneNumber
                  }
                }).then(res2 => {})
              }
              // 登陆成功后跳转到首页
              wx.switchTab({
                url: '/pages/tabbars/home/home'
              })
            }
          })
        }
      })
    }
  },
  // 返回
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      console.log(222);
      var fId = ''
      // 从小程序码进入
      console.log(decodeURIComponent(options.scene))
      var paramsStr = decodeURIComponent(options.scene)
      var paramsArr = paramsStr.split('&');
      for (var i = 0; i < paramsArr.length; i++) {
        var tmp = paramsArr[i].split('=');
        if (tmp[0] == 'fId') {
          fId = tmp[1]
        }
      }
      this.setData({
        fId: fId
      })
    }
    wx.getStorage({
      key: 'codeData',
      success: (res) => {
        console.log(res);
        this.setData({
          openId: res.data.openid,
          key: res.data.session_key
        })
        
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})